package com.goods.controller.business;

import com.goods.business.service.ProductService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 牛俊杰
 */
@Api(tags = "业务模块-物资资料相关接口")
@RestController
@RequestMapping("/business/product")
public class ProductController {

  @Autowired
  private ProductService productService;


  /**
   * 查询所有物资
   * @param pageNum
   * @param pageSize
   * @param categorys
   * @param productVO
   * @return
   */
  @ApiOperation(value = "查询所有物资信息")
  @GetMapping("/findProductList")
  public ResponseBean findProductList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize") Integer pageSize,
                                      @RequestParam(value = "categorys",required = false) String categorys,
                                      ProductVO productVO){
    buildCategorySearch(categorys,productVO);
    PageVO<ProductVO> productVOPageVO = productService.findProductList(pageNum,pageSize,productVO);
    return ResponseBean.success(productVOPageVO);
  }

  private void buildCategorySearch(@RequestParam(value = "categorys",required = false) String categorys, ProductVO productVO) {
    if (categorys!=null&&!"".equals(categorys)){
      String[] split = categorys.split(",");
      switch (split.length){
        case 1:
          productVO.setOneCategoryId(Long.parseLong(split[0]));
          break;
        case 2:
          productVO.setOneCategoryId(Long.parseLong(split[0]));
          productVO.setTwoCategoryId(Long.parseLong(split[1]));
          break;
        case 3:
          productVO.setOneCategoryId(Long.parseLong(split[0]));
          productVO.setTwoCategoryId(Long.parseLong(split[1]));
          productVO.setThreeCategoryId(Long.parseLong(split[2]));
          break;
      }
    }
  }

  /**
   * 添加物资
   * @param productVO
   * @return
   * @throws BusinessException
   */
  @ApiOperation(value = "添加物资信息")
  @PostMapping("/add")
  public ResponseBean add(@RequestBody ProductVO productVO) throws BusinessException {
    if (productVO.getCategoryKeys().length !=3){
      throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资需要3级分类");
    }
    productService.add(productVO);
    return ResponseBean.success();
  }

  @ApiOperation(value = "编辑物资信息",notes = "点击编辑按钮显示物资信息")
  @GetMapping("/edit/{id}")
  public ResponseBean edit(@PathVariable Long id) throws BusinessException {
    ProductVO productVO = productService.edit(id);
    return ResponseBean.success(productVO);
  }

  @ApiOperation(value = "修改物资信息",notes = "点击确定按钮提交修改")
  @PutMapping("/update/{id}")
  public ResponseBean update(@PathVariable Long id,@RequestBody ProductVO productVO) throws BusinessException {
    if (productVO.getCategoryKeys().length !=3){
      throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资需要3级分类");
    }
    productService.update(id,productVO);
    return ResponseBean.success();
  }

  @ApiOperation(value = "移动物资信息",notes = "将物资信息移动至回收站")
  @PutMapping("/remove/{id}")
  public ResponseBean remove(@PathVariable Long id) throws BusinessException {
    productService.remove(id);
    return ResponseBean.success();
  }

  @ApiOperation(value = "恢复物资信息",notes = "恢复回收站中的物资信息")
  @PutMapping("/back/{id}")
  public ResponseBean back(@PathVariable Long id) throws BusinessException {
    productService.back(id);
    return ResponseBean.success();
  }

  @ApiOperation(value = "删除物资信息",notes = "删除回收站中的物资信息")
  @DeleteMapping("/delete/{id}")
  public ResponseBean delete(@PathVariable Long id) throws BusinessException {
    productService.delete(id);
    return ResponseBean.success();
  }

  @ApiOperation(value = "审核物资信息",notes = "点击通过按钮,物资信息移动至正常")
  @PutMapping("/publish/{id}")
  public ResponseBean publish(@PathVariable Long id) throws BusinessException {
    productService.publish(id);
    return ResponseBean.success();
  }

  @ApiOperation(value = "查询物资信息")
  @GetMapping("/findProducts")
  public ResponseBean findProducts(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize")Integer pageSize,
                                   @RequestParam(value = "categorys",required = false)String categorys,
                                   ProductVO productVO){
    productVO.setStatus(0);
    buildCategorySearch(categorys,productVO);
    PageVO<ProductVO> productVOPageVO = productService.findProductList(pageNum,pageSize,productVO);
    return ResponseBean.success(productVOPageVO);
  }

  @ApiOperation(value = "物资库存列表")
  @GetMapping("/findProductStocks")
  public ResponseBean findProductStocks(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                        @RequestParam(value = "pageSize")Integer pageSize,
                                        @RequestParam(value = "categorys",required = false)String categorys,
                                        ProductVO productVO){
    buildCategorySearch(categorys,productVO);
    PageVO<ProductStockVO> productVOPageVO = productService.findProductStocks(pageNum,pageSize,productVO);
    return ResponseBean.success(productVOPageVO);
  }

  @ApiOperation(value = "查询所有库存信息")
  @GetMapping("/findAllStocks")
  public ResponseBean findAllStocks(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                    @RequestParam(value = "pageSize")Integer pageSize,
                                    @RequestParam(value = "categorys",required = false)String categorys,
                                    ProductVO productVO){
    buildCategorySearch(categorys,productVO);
    List<ProductStockVO> productStockVOList = productService.findAllStocks(pageNum,pageSize,productVO);
    return ResponseBean.success(productStockVOList);
  }
}
